<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
if( $action == 'updateprofile' ) {
	$vObj = new CommonValidate();
	$vObj->checkNull($_POST, array('name'));
	if( $vObj->_valid() ) {
		$data_array = array(
				//'users_email_address' => $_POST['field_email_address'],
				//'users_email_public' => $_POST['field_email_public'],
				'users_contactname' => (isset($_POST['field_contactname']) ? $_POST['field_contactname'] : $_POST['field_firstname'] . ' ' . $_POST['field_lastname']),
				'users_firstname' => $_POST['field_firstname'],
				'users_lastname' => $_POST['field_lastname'],
				//'users_company' => $_POST['field_company'],
				//'users_gender' => $_POST['field_gender'],
				//'users_birthday' => $_POST['field_birthday']['Year'] . '-' . $_POST['field_birthday']['Month'] . '-' . $_POST['field_birthday']['Day'],
					);
		$userObj->updateUser($rt_current_userid, $data_array);
		//
		$aid = $userObj->getUserDefaultAddressID($rt_current_userid);
		$data_array = array(
				'address_telephone' => $_POST['field_telephone'],
				'address_fax' => $_POST['field_fax'],
				'address_address' => $_POST['field_address'],
					);
		$userObj->updateAddress($aid, $data_array, $rt_current_userid);
		//
		$feObj->redirect($request_link_current, 's');
	} else {
		$GLOBALS['_SUBMITTED_'] = 1;
		
		$vm = $vObj->_result(':error');
		$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
	}
} elseif( $action == 'updatepassword' || $action == 'changepassword' ) {
	$old_password = util_get_param($_POST, 'field_old_password');
	$new_password = util_get_param($_POST, 'field_new_password');
	$confirm_password = util_get_param($_POST, 'field_confirm_password');
	
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES_VALIDATE . 'account.validate.class.php');
	$vObj = new AccountValidate();
	$vObj->checkPassword($_POST);
	if( $vObj->_valid() ) {
		$valid = $userObj->checkPassword($rt_current_userid, $old_password);
		if( $valid ) {
			$userObj->updatePassword($rt_current_userid, $new_password);
			//
			_dms(':member:success');
			//
			util_redirect($request_link_current . 'success');
		} else {
			$vObj->_add('field_old_password', 'The old password is invalid');
		}
	}
	//
	$vm = $vObj->_result(':error');
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vm', $vm);
	$vr = $vObj->_valid_result();
	$injectObj->addb('func_get_tpl_pagelayout_content', 'content_wrap', 'vr', $vr);
}

//
$member = $memberObj->getCurrentUser();
$feObj->addContent('member', $member);

$address = $userObj->getUserDefaultAddress($rt_current_userid, '', 'address');
$feObj->addContent('address', $address);

//
if( $request_link_subclass == 'password' ) {
	$feObj->addContent('mainpart', 'change_password');
} elseif( $request_link_subclass == 'edit' ) {
	$feObj->addContent('mainpart', 'edit_profile');
}

//
echo $feObj->display('pagelayout/pagelayout_member_profile.tpl');
//
?>